﻿namespace BusinessLogicLayer
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using DataAccessLayer;

    /// <summary>
    /// Describes logic of creating new task dashboard
    /// </summary>
    public class TasksDashBoard: IDashBoard
    {
        /// <summary>
        /// Represents a title of  dashboard
        /// </summary>
        private readonly string title;

        /// <summary>
        /// Represents a dashboard's date of creation
        /// </summary>
        private readonly DateTime createdDate;

        /// <summary>
        /// Represents a dashboard's type
        /// </summary>
        private readonly LayerType type;

        /// <summary>
        /// Represents a list of all tasks in dashboard
        /// </summary>
        private List<WorkItem> tasks;

        /// <summary>
        /// Initializes a new instance of the TasksDashBoard class
        /// without parameters and initialize title, type, creation date
        /// and collection of columns fields.
        /// </summary>
        public TasksDashBoard()
        {
            title = "TasksDashboard";
            type = LayerType.TasksDashBoard;
            createdDate = DateTime.Now;
            Columns = new ColumnCollection(ColumnsAmount.ThreeColumn);
        }

        /// <summary>
        /// Method that initialize list of all tasks from database
        /// </summary>
        public void GetAllItemsList()
        {
            using (var databaseContext = new AdoDbContext())
            {
                tasks = databaseContext.GetTasks();
            }
        }

        /// <summary>
        /// getting task by name
        /// </summary>
        /// <param name="name">name of the task</param>
        /// <returns>instance of WorkItem class by it's name</returns>
        public WorkItem GetItemByName(string name)
        {
            return tasks.FirstOrDefault(workItem => workItem.Name == name);
        }

        /// <summary>
        /// adding new comment to database
        /// </summary>
        /// <param name="taskId"> id of current task</param>
        /// <param name="authorLogin">author's login who have added this comment</param>
        /// <param name="commentBody">the body of new comment</param>
        /// <param name="postDate">the post date of new comment</param>
        /// <returns>comment which was added</returns>
        public Comment AddNewComment(int taskId, string authorLogin, string commentBody, DateTime postDate)
        {
            using (var databaseContext = new AdoDbContext())
            {
                var author = databaseContext.GetUserByLogin(authorLogin);
                var newComment = new Comment(author, commentBody, postDate);
                foreach (var task in tasks)
                {
                    if (task.Id == taskId)
                    {
                        task.Comments.Add(newComment);
                        break;
                    }
                }

                databaseContext.AddComment(taskId, newComment);
                return newComment;
            }
        }

        /// <summary>
        /// This method runs initialization of all columns task's lists
        /// in tasks dashboard
        /// </summary>
        public void RenderColumns()
        {
            renderColumn(Statuses.Done);
            renderColumn(Statuses.InProgress);
            renderColumn(Statuses.New);
        }

        /// <summary>
        /// Initialize specific column's list of tasks 
        /// </summary>
        /// <param name="columnStatus">Indicates which column's list to initialize</param>
        private void renderColumn(Statuses columnStatus)
        {
            var columnTasks =
                from task in Items
                where task.Status == columnStatus
                select task;

            Columns[columnStatus].ColumnItems = (List<WorkItem>)columnTasks.ToList();
        }

        /// <summary>
        /// Gets or sets columns in column collection
        /// </summary>
        public ColumnCollection Columns { get; set; }

        /// <summary>
        /// Gets list of all tasks in dashboard
        /// </summary>
        public List<WorkItem> Items
        {
            get { return tasks; }
        }

        /// <summary>
        /// Gets title of tasks dashboard
        /// </summary>
        public string Title
        {
            get { return title; }
        }

        /// <summary>
        /// Gets date of creation of tasks dashboard
        /// </summary>
        public DateTime CreatedDate
        {
            get { return createdDate; }
        }

        /// <summary>
        /// Gets type of tasks dashboard
        /// </summary>
        public LayerType Type
        {
            get { return type; }
        }
    }
}
